package cn.com.darly.videoandroidpm.ui.conversation.video;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.darly.common.DLog;
import com.darly.common.ToastApp;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;
import com.darly.dview.widget.camera.util.ImageLoaderUtil;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.SharePreferHelp;
import cn.com.darly.videoandroidpm.common.StaticListener;
import cn.com.darly.videoandroidpm.common.StaticListener.OnConnectVideoListener;
import cn.com.darly.videoandroidpm.common.VpmTimer;
import cn.com.darly.videoandroidpm.ui.biz.VideoStartCallListener.VideoStartCallInterface;
import cn.com.darly.videoandroidpm.ui.presenter.VideoStartCallPresenter;

/** 主动发起呼叫的类，展示用户呼叫对方页面。
 * @author Darly/张宇辉/2018/5/29 9:17
 * @version 1.0/cn.com.darly.videoandroidpm.ui.conversation.video
 */
@ContentBinder(R.layout.activity_video_startcall)
public class VideoStartCallActivity extends BaseActivity implements View.OnClickListener,VideoStartCallInterface,OnConnectVideoListener{

    @ViewsBinder(R.id.id_video_start_msg)
    TextView id_video_start_msg;

    @ViewsBinder(R.id.id_video_start_icon)
    ImageView id_video_start_icon;
    @ViewsBinder(R.id.id_video_start_name)
    TextView id_video_start_name;
    @ViewsBinder(R.id.id_video_start_stop)
    Button id_video_start_stop;

    private LoginInfo friend;

    private LoginInfo user;

    private VideoStartCallPresenter presenter;

    private String ownRtmp;
    @Override
    protected void initView(Bundle savedInstanceState) {
        friend = (LoginInfo) getIntent().getSerializableExtra("LoginInfo");
        if (friend==null) {
            ToastApp.showToast("好友信息为空");
            finish();
        }
        user = (LoginInfo) SharePreferHelp.getValue("LoginInfo");
        if (user == null){
            ToastApp.showToast("用户信息错误");
            finish();
        }
        //用户信息都没有问题的情况下，发送请求到服务端。由服务端进行视频通话请求发起。
        presenter = new VideoStartCallPresenter(this);
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("friendid", friend.getId());
        presenter.onStartCall(params);
    }

    @Override
    protected void loadData() {
        ImageLoaderUtil.getInstance().loadImageNor(friend.getIcon(),id_video_start_icon);
        id_video_start_name.setText(friend.getName());
    }

    @Override
    protected void initListener() {
        id_video_start_stop.setOnClickListener(this);
        StaticListener.setOnConnectVideoListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.id_video_start_stop:
                VpmTimer.getInstance().stopTimer(friend.getId());
                Map<String, String> params = new LinkedHashMap<String, String>();
                params.put("friendid", friend.getId());
                presenter.onStopCall(params);
                end();
                break;
        }
    }

    @Override
    public void onVideoStartCallSuccess(String  info) {
        if (TextUtils.isEmpty(info)){
            return;
        }
        ownRtmp = info;
        //启动定时器，好友100秒内没有响应的话，自动关闭。
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //开始计时
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastApp.showToast("对方正在忙碌，或者对方未带手机，请稍后再呼叫...");
                        VpmTimer.getInstance().stopTimer(friend.getId());
                        Map<String, String> params = new LinkedHashMap<String, String>();
                        params.put("friendid", friend.getId());
                        presenter.onStopCall(params);
                        end();
                    }
                });
            }
        },10*1000);
        VpmTimer.Timers.put(friend.getId(),timer);
    }

    @Override
    public void onVideoStartCallFailed(String info) {

    }

    @Override
    public void end() {
        finish();
    }

    @Override
    public void setDisableClick() {

    }

    @Override
    public void setEnableClick() {

    }

    @Override
    public void connect(String key,String rtmp) {
        if ("400".equals(key)){
            //对方接受视频通话
            VpmTimer.getInstance().stopTimer(friend.getId());
            id_video_start_msg.setText(friend.getName() + "接通" + user.getName() + "的视频请求");
            //成功连接后，跳转页面。
            Intent intent = new Intent(this,VideoConnectionActivity.class);
            intent.putExtra("LoginInfo",friend);
            intent.putExtra("OWN",ownRtmp);//这个是自己的流媒体地址
            intent.putExtra("FWN",rtmp);//这个是好友的流媒体地址
            startActivity(intent);
            DLog.i(getClass().getSimpleName()+"connect 执行了一次");
            end();
        }else if ("401".equals(key)) {
            //对方拒绝视频通话，提示用户，并关闭页面
            ToastApp.showToast(friend.getName()+"挂断了您的视频请求，请稍后再试！");
            VpmTimer.getInstance().stopTimer(friend.getId());
            end();
        }
    }
}
